package com.ziyueyuwen.xiniao.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ResourceBundle;

/**
 * @author jun 2019/1/29.
 */
public class ChineseUtil {
    private static ResourceBundle zh2HansMap;
    private static ResourceBundle zh2HantMap;

    static {
        zh2HansMap = ResourceBundle.getBundle("config/zh2Hans");
        zh2HantMap = ResourceBundle.getBundle("config/zh2Hant");
    }

    public static String toSimplified(String srcStr) {
        return converter(srcStr, zh2HansMap);
    }

    public static String toTraditional(String srcStr) {
        return converter(srcStr, zh2HantMap);
    }

    private static String converter(String srcStr, ResourceBundle resourceBundle) {
        if (StringUtils.isBlank(srcStr)) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        char[] chars = srcStr.toCharArray();
        for (char c : chars) {
            String tmpStr = String.valueOf(c);
            String changedStr = resourceBundle.containsKey(tmpStr) ? resourceBundle.getString(tmpStr) : tmpStr;
            stringBuilder.append(changedStr);
        }
        return stringBuilder.toString();
    }
}
